import java.util.Random;


public class Firefly {
	private double[] position;
	private double intensity;
	private FireflySet set;
	private int id;
	private Random r;
	
	public Firefly(int dimension, FireflySet s, int id) {
		position = new double[dimension];
		set = s;
		this.id = id;
		r = new Random();
	}
	
	public int getId() {
		return id;
	}
	
	public double[] getPosition() {
		return position;
	}
	
	public double getIntensity() {
		return intensity;
	}
	
	public void setPosition(double[] pos) {
		position = pos;
		intensity = set.getFunction().intensity(pos);
	}
	
	public void updatePosition(Firefly towards) {
		double distance = 0.0;
		double[] partnerPosition = towards.getPosition();
		for (int i = 0; i < position.length; i++)
			distance += Math.pow(position[i] - partnerPosition[i], 2);
		distance = Math.sqrt(distance);
		double distanceFactor = set.getAttractiveness() * Math.exp(-set.getAbsorption() * Math.pow(distance, 2));
		for (int i = 0; i < position.length; i++)
			position[i] += (partnerPosition[i] - position[i]) * distanceFactor + set.getRandomness() * (r.nextDouble() - 0.5);
		intensity = set.getFunction().intensity(position);
	}
}
